import requests
import pprint

headers = {'Host': '192.168.1.100:9200',
           'Content-Type': 'application/json'}


# 创建索引
def init_index(index_name, shards, replicas):
    init_index_json = {
        "settings": {
                "number_of_shards": shards,
                "number_of_replicas": replicas
            },

        "mappings": {
            "pkts": {
                "properties": {
                    "sniff_time": {"type": "date"}
                }
            }
        }
    }
    r = requests.put('http://192.168.1.100:9200/' + index_name + '/', headers=headers, json=init_index_json)

    pprint.pprint(r.json())


def get_index_info(index_name):
    if index_name == 'all':
        r = requests.get('http://192.168.1.100:9200/_all/_settings', headers=headers)
    else:
        r = requests.get('http://192.168.1.100:9200/' + index_name + '/_settings', headers=headers)

    pprint.pprint(r.json())


def delete_index(index_name):
    r = requests.delete('http://192.168.1.100:9200/' + index_name, headers=headers)
    pprint.pprint(r.json())


if __name__ == "__main__":
    init_index('pyshark_new', 5, 1)
    # get_index_info('all')
    # delete_index('pyshark_new')